package com.rs.portal.reserve.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.web.servlet.ModelAndView;

import com.jaxplat.JaxplatBean;
import com.jaxplat.commons.Row;
import com.jaxplat.commons.RowImpl;
import com.rs.commons.RsConstant;
import com.rs.commons.web.PortalBaseAction;
import com.rs.rsbiz.reserve.delegate.ReserveOrderDelegate;
import com.rs.rsbiz.services.ReserveFacade;
import com.rs.rsbiz.services.ReserveFactory;
import com.rs.rsbiz.usercontract.delegate.UserContractDelegate;
import com.rs.util.Pagination;

/**
 * 预约管理
 * 
 * @author Administrator
 * @date 2013-3-8
 */
public class ReserveManageAction extends PortalBaseAction
{
    private ReserveOrderDelegate reserveOrderDelegate;
    private UserContractDelegate userContractDelegate;

    public UserContractDelegate getUserContractDelegate()
    {
        return userContractDelegate;
    }

    public void setUserContractDelegate(UserContractDelegate userContractDelegate)
    {
        this.userContractDelegate = userContractDelegate;
    }

    public ReserveOrderDelegate getReserveOrderDelegate()
    {
        return reserveOrderDelegate;
    }

    public void setReserveOrderDelegate(ReserveOrderDelegate reserveOrderDelegate)
    {
        this.reserveOrderDelegate = reserveOrderDelegate;
    }

    /**
     * 预约管理订单查询
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView reserverOrderList(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Map resultMap = new HashMap();
        String pagingHtml = "";
        String returnView = "reserve_list";
        Row params = this.parseRequestParametersToRow(request);
        String flag = params.getString("flag");
        // 获取当前登录用户信息
        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("mobile");
        
        //获取常用联系人的话从这里直接返回
        if("4".equals(params.getString("flag"))){
            if (null != userId && !"".equals(userId))
            {
                userContractDelegate = (UserContractDelegate) JaxplatBean.getBean("userContractDelegate");
                List rows =  userContractDelegate.getContractsOfUser(userId);
                resultMap.put("flag", flag);
                resultMap.put("userContrctList", rows);
                return new ModelAndView(returnView, resultMap);
            }
        }
        int page = 0;
        if (null != params.getString("page") && !"".equals(params.getString("page")))
        {
            page = Integer.parseInt(params.getString("page"));
        }
        // 浏览方式browseFalg
        String browseFalg = params.getString("browseFalg");
        params.put("pageNumber", page == 0 ? 1 : page);
        params.put("pageSize", params.getInt("rows") == 0 ? 10 : params.getInt("rows"));
        params.put("pageIndex", params.getInt("pageNumber"));
        params.put("userId", userId);

        String action = request.getRequestURI() + "?method=reserverOrderList&flag=" + flag;
        if (null != userId && !"".equals(userId))
        {
            String orderId = params.getString("orderId");
            Object[] result = this.getReserveOrderDelegate().getReserveOrderList(params);
            List rows = (List) result[0];
            Integer totalCount = (Integer) result[1];
            if (null != orderId && !"".equals(orderId))
            {
                returnView = "reserve_info";
            }
            // 取得总记录数
            int pageIndex = 1;
            if (page != 0)
            {
                pageIndex = page;
            }
            // 处理分页
            Pagination pagination = new Pagination();
            if ("web".equals(browseFalg))
            {
                pagingHtml = pagination.getGoogleToolsBar(totalCount.intValue(), "doPagination", pageIndex, RsConstant.PAGESIZE);
            }
            else if(request.getRequestURL().indexOf("wap3")>0)
            {
                pagingHtml = pagination.getPageTagToTouch(totalCount.intValue(), RsConstant.PAGESIZE, pageIndex, action);
            }else{
                pagingHtml = pagination.getPageTag(totalCount.intValue(), RsConstant.PAGESIZE, pageIndex, action);
            }
            resultMap.put("totalCount", totalCount.intValue());
            resultMap.put("reserveOrderList", rows);
            resultMap.put("pagingHtml", pagingHtml);
            resultMap.put("orderId", orderId);
            resultMap.put("flag", flag);
        }
        return new ModelAndView(returnView, resultMap);
    }

    // 查看订单详细信息
    public ModelAndView viewReserveOrder(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Map resultMap = new HashMap();
        Row params = this.parseRequestParametersToRow(request);
        if (null != request.getSession().getAttribute("mobile"))
        {
            String userId = request.getSession().getAttribute("mobile").toString();
            params.put("userId", userId);
            String orderId = params.getString("orderId");
            // 查询就医信息申请人信息
            Row orderInfo = this.getReserveOrderDelegate().getReserveOrderInfo(params);
            // 查询付款信息
            List orderBillInfoList = this.getReserveOrderDelegate().getReserveOrderBillInfo(params);
            Row orderBillInfo = null;
            if (null != orderBillInfoList) {
                if (orderBillInfoList.size() > 1) {
                    orderBillInfo = (Row)orderBillInfoList.get(1);
                } else {
                    orderBillInfo = (Row)orderBillInfoList.get(0);
                }
            }
            resultMap.put("orderInfo", orderInfo);
            resultMap.put("orderBillInfo", orderBillInfo);
            resultMap.put("orderId", orderId);
            resultMap.put("flag", params.getString("flag"));
        }
        else
        {
            resultMap.put("message", "请先登录后再操作！");
        }
        return new ModelAndView("reserve_info", resultMap);
    }

    /**
     * 退号
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView refund(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        String flag = param.getString("flag");
        // 浏览方式browseFalg
        String browseFalg = param.getString("browseFalg");
        long orderId = param.getLong("orderId");
        param.addColumn("userid", request.getSession().getAttribute("mobile"));
        ReserveFacade facade = ReserveFactory.getReserveFacade();
        Row result = facade.refund(param);
        // 提示信息
        String message = (String) result.get("message");
        if (!"wap".equals(browseFalg))
        {
            JSONObject json = new JSONObject();
            // 这里必须写死为rows和total
            // 否则页面不显示
            json.putAll(result);
            response.setContentType("text/html;charset=UTF-8");
            /*if ("1".equals(flag))
            {

                response.getWriter().print(json.toString());
            }
            else
            {
                response.getWriter().print(json.toString());
            }*/
            response.getWriter().print(json.toString());
            response.getWriter().flush();
            response.getWriter().close();
            return null;
        }
        else
        { // 返回wap页面
            Map resultMap = new HashMap();
            resultMap.put("message", message);
            resultMap.put("flag", flag);
            resultMap.put("orderId", param.getString("orderId"));
            return new ModelAndView("error", resultMap);
        }
    }

    /**
     * 取消订单
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView cancelOrder(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        String flag = param.getString("flag");
        // 浏览方式browseFalg
        String browseFalg = param.getString("browseFalg");
        long orderId = param.getLong("orderId");
       
        param.addColumn("userid", request.getSession().getAttribute("mobile"));
        Row result = this.getReserveOrderDelegate().cancelOrder(param);
        Row orderInfo = this.getReserveOrderDelegate().getReserveOrderInfo(param);
        // 提示信息
        String message = (String) result.get("message");
        if (!"wap".equals(browseFalg))
        {
            JSONObject json = new JSONObject();
            // 这里必须写死为rows和total
            // 否则页面不显示
            json.putAll(result);
            response.setContentType("text/html;charset=UTF-8");
            if ("1".equals(flag))
            {

                response.getWriter().print(json.toString());
            }
            else
            {
                response.getWriter().print(json.toString());
            }
            response.getWriter().flush();
            response.getWriter().close();
            return null;
        }
        else
        { // 返回wap页面
            Map resultMap = new HashMap();
            resultMap.put("message", message);
            resultMap.put("flag", flag);
            resultMap.put("orderId", param.getString("orderId"));
            request.setAttribute("orderInfo", orderInfo);
            return new ModelAndView("reserve_info", resultMap);
        }
    }

    public ModelAndView deleteUserContract(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        userContractDelegate = (UserContractDelegate) JaxplatBean.getBean("userContractDelegate");
        userContractDelegate.deleteUserContract(param);
        JSONObject json = new JSONObject();
        response.setContentType("text/html;charset=UTF-8");
        json.put("flag", "4");
        response.getWriter().print(json.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }

}
